package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.radio.NxRadio;
	import newx.com.control.radio.NxRadioEvent;
	import newx.com.control.table.NxTable;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;
	
	public class BossTimeSetUI extends BaseUI
	{
		private static var self:BossTimeSetUI;
		private var bossTimeObj:Object;
		private var totalDays:int	=	7;
		private var canChooseNum:int	=	2;
		private var radioSelectNum:int	=	0;
		private var model:ModelLocator;
		private var chooseArr:Array	=	[];
		private var isFaction:int	=	-1;
		private var setArr:Array	=	[];
		
		public var title:NxButton;
		public var bg_right:MovieClip;
		public var describe:NxLabel;
		public var submitBtn:NxButton;
		public var closeBtn:NxButton;
		public var table:NxTable;
		
		public var radio1:NxRadio;
		public var radio2:NxRadio;
		public var radio3:NxRadio;
		public var radio4:NxRadio;
		public var radio5:NxRadio;
		public var radio6:NxRadio;
		public var radio7:NxRadio;
		
		public var label1:NxLabel;
		public var label2:NxLabel;
		public var label3:NxLabel;
		public var label4:NxLabel;
		public var label5:NxLabel;
		public var label6:NxLabel;
		public var label7:NxLabel;
		
		public var choose1:NxLabel;
		public var choose2:NxLabel;
		public var choose3:NxLabel;
		public var choose4:NxLabel;
		public var choose5:NxLabel;
		public var choose6:NxLabel;
		public var choose7:NxLabel;
		
		
		
		public function BossTimeSetUI(popupUI:Boolean=true)
		{
			super(popupUI);
		}
		
		public static function getInstance():BossTimeSetUI
		{
			if(self == null)
			{
				self 		= 	new BossTimeSetUI();
			}
			return self;
		}
		
		override public function Init():void
		{
			model	=	ModelLocator.getInstance();
			title.buttonMode	=	title.mouseChildren	=	false;
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_BANDITI_INFO),freshSet);
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_BANDITI_APPLY_WEEKINDEX_INDEX),onSetRadioEnable);
		}
		
		private function freshSet(e:CustomMsgEvent):void
		{
			reSetRadio();
			setArr	=	e.pList;
			for(var i:int=0;i<3;i+=2)
			{
				if(setArr[i] == 0) return;
				if( this[ "radio"+ setArr[i] ] != null )
				{
					(this["radio"+ setArr[i] ] as NxRadio).selected 	= true;
					(this["choose"+ setArr[i] ] as NxLabel).htmlText	= setArr[i+1]==1?StaticIni.getSwfLangStr("UIString_1000624"):StaticIni.getSwfLangStr("UIString_1000370");
					(this["radio"+ setArr[i] ] as NxRadio).enable		= setArr[i+1]==1?false:true
				}
			}
			
			freshSelected();
		}
		
		private function onSetRadioEnable(e:CustomMsgEvent):void
		{
			var arr:Array	=	e.pList;
			var length:int	=	arr.length;
			for(var i:int=0;i<length;i+=2)
			{
				(this["radio"+ arr[i] ] as NxRadio).enable 	= arr[i+1]== 1 ? true : false;
			}
			
			initUI();
		}
		
		private function reSetRadio():void
		{
			for(var i:int=1;i<=totalDays;i++)
			{
				(this["radio"+i] as NxRadio).selected		=	false;
				(this["radio"+i] as NxRadio).visible		=	true;
			}
		}
		
		override protected function BeforeShow():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_BANDITI_GET_INFO);
			isFaction	=	model.scene.Role.GetProperties("GuildPosition");
			if(bossTimeObj==null) bossTimeObj	=	StaticIni.getIniObj("BossTimeSet");
			addEvents();
		}
		
		override protected function AfterHide():void
		{
			reSetRadio();
			removeEvents();
			chooseArr=[];
			radioSelectNum=0;
		}
		
		private function initUI():void
		{
			for(var i:int=1;i<=totalDays;i++)
			{
				(this["label"+i] as NxLabel).htmlText	=	StaticIni.getSwfLangStr("UIString_100036"+(i-1));//bossTimeObj["day"+i] + ":00";
				(this["choose"+i] as NxLabel).visible		=	false;
				if(isFaction != 1)
				{
					(this["radio"+i] as NxRadio).enable	=	false;
				}
			}
		}
		
		
		private function addEvents():void
		{
			for(var i:int=1;i<=totalDays;i++)
			{
				(this["radio"+i] as NxRadio).addEventListener(NxRadioEvent.RADIO_CLICK,onRadioClick);
			}
			windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			closeBtn.addEventListener(MouseEvent.CLICK,function close(e:MouseEvent):void{onClose()});
			submitBtn.addEventListener(MouseEvent.CLICK,onSubmit);
		}
		
		private function removeEvents():void
		{
			for(var i:int=1;i<=totalDays;i++)
			{
				(this["radio"+i] as NxRadio).removeEventListener(NxRadioEvent.RADIO_CLICK,onRadioClick);
			}
			windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			submitBtn.removeEventListener(MouseEvent.CLICK,onSubmit);
		}
		
		private function onClose(e:NxWindowEvent=null):void
		{
			hide();
		}
		
		private function onSubmit(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_BANDITI_SET_INFO,chooseArr[0]?chooseArr[0]:8,chooseArr[1]?chooseArr[1]:8);
			reSetRadio();
//			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_BANDITI_GET_INFO);
		}
		
		private function onRadioClick(e:NxRadioEvent):void
		{
			freshSelected();
		}
		
		private function freshSelected():void
		{
			for(var i:int=1;i<=totalDays;i++)
			{
				if( (this["radio"+i] as NxRadio).selected )
				{
					(this["choose"+i] as NxLabel).visible	=	true;
					if(chooseArr.indexOf(i) == -1)
					{
						chooseArr.push(i);
						radioSelectNum++;
					}
				}
				else
				{
					(this["choose"+i] as NxLabel).visible	=	false;
					var index:int	=	chooseArr.indexOf(i);
					if( index != -1 )
					{
						radioSelectNum--;
						chooseArr.splice( index , 1);
					}
				}
			}
			if(radioSelectNum == canChooseNum)
			{
				for(var j:int=1;j<=totalDays;j++)
				{
					if( !(this["choose"+j] as NxLabel).visible )
					{
						(this["radio"+j] as NxRadio).visible = false;
					}
				}
			}
			else
			{
				for(var k:int=1;k<=totalDays;k++)
				{
					(this["radio"+ k ] as NxRadio).visible		=	true;
				}
			}
			
		}
		
	}
}